package com.industrial.im.service.impl;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.industrial.common.core.utils.DateUtils;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.im.domain.DispatchKeyItemManagement;
import com.industrial.im.mapper.DispatchKeyItemManagementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.industrial.im.mapper.DispatchApplyExtensionMapper;
import com.industrial.im.domain.DispatchApplyExtension;
import com.industrial.im.service.IDispatchApplyExtensionService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 申请延期Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-11
 */
@Service
public class DispatchApplyExtensionServiceImpl implements IDispatchApplyExtensionService 
{
    @Autowired
    private DispatchApplyExtensionMapper dispatchApplyExtensionMapper;

    @Autowired
    private DispatchKeyItemManagementMapper dispatchKeyItemManagementMapper;

    /**
     * 查询申请延期
     * 
     * @param baseId 申请延期主键
     * @return 申请延期
     */
    @Override
    public DispatchApplyExtension selectDispatchApplyExtensionByBaseId(String baseId)
    {
        DispatchApplyExtension dispatchApplyExtension = dispatchApplyExtensionMapper.selectDispatchApplyExtensionByBaseId(baseId);
        if(dispatchApplyExtension != null){
            String pkId = dispatchApplyExtension.getPkId();
            DispatchKeyItemManagement dispatchKeyItemManagement = dispatchKeyItemManagementMapper.selectDispatchKeyItemManagementByBaseId(pkId);

            if(dispatchKeyItemManagement != null){
                dispatchApplyExtension.setOrStartTime(dispatchKeyItemManagement.getStartTime());
                dispatchApplyExtension.setOrEndTime(dispatchKeyItemManagement.getEndTime());
                dispatchApplyExtension.setItemName(dispatchKeyItemManagement.getItemName());
            }

        }



        return dispatchApplyExtension;
    }

    /**
     * 查询申请延期列表
     * 
     * @param dispatchApplyExtension 申请延期
     * @return 申请延期
     */
    @Override
    public List<DispatchApplyExtension> selectDispatchApplyExtensionList(DispatchApplyExtension dispatchApplyExtension)
    {
        return dispatchApplyExtensionMapper.selectDispatchApplyExtensionList(dispatchApplyExtension);
    }

    /**
     * 新增申请延期
     * 
     * @param dispatchApplyExtension 申请延期
     * @return 结果
     */
    @Override
    public int insertDispatchApplyExtension(DispatchApplyExtension dispatchApplyExtension)
    {
        dispatchApplyExtension.setBaseId(IdUtil.simpleUUID());
        dispatchApplyExtension.setBaseCreateTime(DateUtils.getNowDate());
        dispatchApplyExtension.setBaseCreateBy(SecurityUtils.getUserId());
        dispatchApplyExtension.setBaseUpdateTime(DateUtils.getNowDate());
        dispatchApplyExtension.setBaseUpdateBy(SecurityUtils.getUserId());
        dispatchApplyExtension.setStatus("0");
        return dispatchApplyExtensionMapper.insertDispatchApplyExtension(dispatchApplyExtension);
    }

    /**
     * 修改申请延期
     * 
     * @param dispatchApplyExtension 申请延期
     * @return 结果
     */
    @Override
    public int updateDispatchApplyExtension(DispatchApplyExtension dispatchApplyExtension)
    {
        return dispatchApplyExtensionMapper.updateDispatchApplyExtension(dispatchApplyExtension);
    }

    /**
     * 批量删除申请延期
     * 
     * @param baseIds 需要删除的申请延期主键
     * @return 结果
     */
    @Override
    public int deleteDispatchApplyExtensionByBaseIds(String[] baseIds)
    {
        return dispatchApplyExtensionMapper.deleteDispatchApplyExtensionByBaseIds(baseIds);
    }

    /**
     * 删除申请延期信息
     * 
     * @param baseId 申请延期主键
     * @return 结果
     */
    @Override
    public int deleteDispatchApplyExtensionByBaseId(String baseId)
    {
        return dispatchApplyExtensionMapper.deleteDispatchApplyExtensionByBaseId(baseId);
    }

    /**
     * @param dispatchApplyExtension
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void check(DispatchApplyExtension dispatchApplyExtension) {
        dispatchApplyExtensionMapper.check(dispatchApplyExtension);
        if(StringUtils.equals(dispatchApplyExtension.getStatus(),"1")){
            //更新结束时间
            DispatchKeyItemManagement updateParams = new DispatchKeyItemManagement();
            updateParams.setBaseId(dispatchApplyExtension.getPkId());
            updateParams.setEndTime(dispatchApplyExtension.getEndTime());
            dispatchKeyItemManagementMapper.updateDispatchKeyItemManagementEndTime(updateParams);
        }
    }
}
